package com.baihang.framework.dao;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import com.baihang.framework.annotation.Column;
import com.baihang.framework.annotation.Table;
import com.baihang.framework.model.BaseModel;

public class ModelToSql {
	public static String modelToSaveSql(BaseModel model) {
		StringBuilder sqlCol = new StringBuilder("");
		StringBuilder sqlVal = new StringBuilder("");
		
		Class<? extends BaseModel> clazz = model.getClass(); //获取反射对象
		Annotation[] classAnn =clazz.getAnnotations();//获取类的注解数组
		Table tab = (Table)classAnn[0];
		
		String tableName = tab.name(); //获得表名
		
		Field[] fields = clazz.getDeclaredFields();//获取所有属性值
		for(Field field : fields) {
			field.setAccessible(true);//允许访问私有属性
			Annotation[] annotations = field.getAnnotations();//获得属性的注解数组
			Column col = (Column)annotations[0];	//实例一个注解
			try {
				if(field.get(model) !=null){
					sqlCol.append(col.name()+",");
					sqlVal.append("'"+field.get(model)+"',");					
				}else {
					continue;
				}				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//显示属性名称，属性值，注解值
		}
		String saveSql=String.format("insert into %s (%s) values (%s)", tableName,sqlCol.toString().substring(0, sqlCol.toString().length()-1),sqlVal.toString().substring(0, sqlVal.toString().length()-1));
		return saveSql;
	}
	
	public static String modelToUpdateSql(BaseModel model) {
		StringBuilder sqlMap = new StringBuilder("");
		
		Class<? extends BaseModel> clazz = model.getClass(); //获取反射对象
		Annotation[] classAnn =clazz.getAnnotations();//获取类的注解数组
		Table tab = (Table)classAnn[0];
		
		String tableName = tab.name(); //获得表名
		String id="";//主键
		Field[] fields = clazz.getDeclaredFields();//获取所有属性值
		for(Field field : fields) {
			field.setAccessible(true);//允许访问私有属性
			Annotation[] annotations = field.getAnnotations();//获得属性的注解数组
			Column col = (Column)annotations[0];	//实例一个注解
			try {
				if(field.get(model) !=null){
					if(field.getName()=="ID") {
						id= (String)field.get(model);
						continue;
					}
					sqlMap.append(col.name()+"='"+field.get(model)+"',");
				}else {
					continue;
				}				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//显示属性名称，属性值，注解值
		}
		String updateSql=String.format("update %s set %s where id='%s' ", tableName,sqlMap.toString().substring(0, sqlMap.toString().length()-1),id);
		return updateSql;
	}
	
	public static String modelToUpdateSql2(BaseModel model) {
		StringBuilder sqlMap = new StringBuilder("");
		
		Class<? extends BaseModel> clazz = model.getClass(); //获取反射对象
		Annotation[] classAnn =clazz.getAnnotations();//获取类的注解数组
		Table tab = (Table)classAnn[0];
		
		String tableName = tab.name(); //获得表名
		int id=0;//主键值
		String fkCol="";//主键注解名
		Field[] fields = clazz.getDeclaredFields();//获取所有属性值
		for(Field field : fields) {
			field.setAccessible(true);//允许访问私有属性
			Annotation[] annotations = field.getAnnotations();//获得属性的注解数组
			Column col = (Column)annotations[0];	//实例一个注解
			try {
				if(field.get(model) !=null){
					if(field.getName()=="ID") {
						id= (Integer)field.get(model);
						fkCol= col.name();
						continue;
					}
					sqlMap.append(col.name()+"='"+field.get(model)+"',");
				}else {
					continue;
				}				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//显示属性名称，属性值，注解值
		}
		String updateSql=String.format("update %s set %s where %s=%d ", tableName,sqlMap.toString().substring(0, sqlMap.toString().length()-1),fkCol,id);
		System.err.println("updatesql="+updateSql);
		return updateSql;
	}
}
